library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 9.1
summary(factor(df$Q5A))
summary(factor(df$Q6A))
df2 <- df

# Important
df2$Q5A[df2$Q5A>3] <- NA
df2$Q5A[df2$Q5A==3] <- 0
df2$Q5A[df2$Q5A==2] <- 0.5
df2$Q5A[df2$Q5A==1] <- 1

df2$Q5B[df2$Q5B>3] <- NA
df2$Q5B[df2$Q5B==3] <- 0
df2$Q5B[df2$Q5B==2] <- 0.5
df2$Q5B[df2$Q5B==1] <- 1

df2$Q5C[df2$Q5C>3] <- NA
df2$Q5C[df2$Q5C==3] <- 0
df2$Q5C[df2$Q5C==2] <- 0.5
df2$Q5C[df2$Q5C==1] <- 1

df2$Q5D[df2$Q5D>3] <- NA
df2$Q5D[df2$Q5D==3] <- 0
df2$Q5D[df2$Q5D==2] <- 0.5
df2$Q5D[df2$Q5D==1] <- 1

df2$Q5E[df2$Q5E>3] <- NA
df2$Q5E[df2$Q5E==3] <- 0
df2$Q5E[df2$Q5E==2] <- 0.5
df2$Q5E[df2$Q5E==1] <- 1

df2$Q5F[df2$Q5F>3] <- NA
df2$Q5F[df2$Q5F==3] <- 0
df2$Q5F[df2$Q5F==2] <- 0.5
df2$Q5F[df2$Q5F==1] <- 1

df2$Q5G[df2$Q5G>3] <- NA
df2$Q5G[df2$Q5G==3] <- 0
df2$Q5G[df2$Q5G==2] <- 0.5
df2$Q5G[df2$Q5G==1] <- 1

df2$Q5H[df2$Q5H>3] <- NA
df2$Q5H[df2$Q5H==3] <- 0
df2$Q5H[df2$Q5H==2] <- 0.5
df2$Q5H[df2$Q5H==1] <- 1

colnames(df2[,25:32])
df2$IMPORTANT <- rowSums(df2[,25:32],na.rm = FALSE)/8
summary(df2$IMPORTANT)

# Useful
df2$Q6A[df2$Q6A>3] <- NA
df2$Q6A[df2$Q6A==3] <- 0
df2$Q6A[df2$Q6A==2] <- 0.5
df2$Q6A[df2$Q6A==1] <- 1

df2$Q6B[df2$Q6B>3] <- NA
df2$Q6B[df2$Q6B==3] <- 0
df2$Q6B[df2$Q6B==2] <- 0.5
df2$Q6B[df2$Q6B==1] <- 1

df2$Q6C[df2$Q6C>3] <- NA
df2$Q6C[df2$Q6C==3] <- 0
df2$Q6C[df2$Q6C==2] <- 0.5
df2$Q6C[df2$Q6C==1] <- 1

df2$Q6D[df2$Q6D>3] <- NA
df2$Q6D[df2$Q6D==3] <- 0
df2$Q6D[df2$Q6D==2] <- 0.5
df2$Q6D[df2$Q6D==1] <- 1

df2$Q6E[df2$Q6E>3] <- NA
df2$Q6E[df2$Q6E==3] <- 0
df2$Q6E[df2$Q6E==2] <- 0.5
df2$Q6E[df2$Q6E==1] <- 1

df2$Q6F[df2$Q6F>3] <- NA
df2$Q6F[df2$Q6F==3] <- 0
df2$Q6F[df2$Q6F==2] <- 0.5
df2$Q6F[df2$Q6F==1] <- 1

df2$Q6G[df2$Q6G>3] <- NA
df2$Q6G[df2$Q6G==3] <- 0
df2$Q6G[df2$Q6G==2] <- 0.5
df2$Q6G[df2$Q6G==1] <- 1

df2$Q6H[df2$Q6H>3] <- NA
df2$Q6H[df2$Q6H==3] <- 0
df2$Q6H[df2$Q6H==2] <- 0.5
df2$Q6H[df2$Q6H==1] <- 1


colnames(df2[,33:40])
df2$USEFUL <- rowSums(df2[,33:40], na.rm=FALSE)/8
summary(df2$USEFUL)

df2 <- df2[df2$Q11 < 5 & df2$P_ASSIGN1==1,]

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

f.imp <- svyby(~ IMPORTANT, ~Q11, w2_design, svymean, na.rm = TRUE)
f.use <- svyby(~ USEFUL, ~Q11, w2_design, svymean, na.rm = TRUE)


# Important
g.imp <- ggplot(f.imp, aes(x=factor(Q11),y=IMPORTANT,label=round(IMPORTANT,digits=2)))+
  geom_col(width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_x_discrete(breaks=c(1,2,3,4),labels=c("A great deal",'Quite a lot',"Some",'Very little'))+
  xlab("")+
  ylab("Importance of Military Missions\n")

# Useful
g.use <- ggplot(f.use, aes(x=factor(Q11),y=USEFUL,label=round(USEFUL,digits=2)))+
  geom_col(width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  scale_x_discrete(breaks=c(1,2,3,4),labels=c("A great deal",'Quite a lot',"Some",'Very little'))+
  xlab("")+
  ylab("Usefulness of Military Missions\n")

grid.arrange(g.imp,g.use,ncol=2)
